<?php
/******************************
 *
 * 时间：2017年12月15日
 * 功能：查看老师详情和举报老师
 * 作者：Mr Peng
 *
 *****************************/ 
namespace Api\Controller;
use Think\Controller;
header('Content-Type:text/json;charset=utf-8');
header("Access-Control-Allow-Origin: *");
class TeacherController extends CommonController {

// +----------------------------------------------------------------------
// | 功能       | 老师详情
// +----------------------------------------------------------------------
// | 请求类型   | post
// +----------------------------------------------------------------------
// | 参数       | teacher_id
// +----------------------------------------------------------------------    
	public function index(){
		if(IS_POST){
			$id=I('teacher_id');
			if($id==''){
				$data=(object)null;
				echo json_encode($this->apiTemplate($data,'204','参数错误'));
			}else{
				$userrel=M('user')->where(array('user_id'=>$id))->find();
				if($userrel){
					$rel=M('userdata')->where(array('uid'=>$id))->find();
					$data['teacher_id']=$id;
					$data['img']=URL.$rel['image'];
					$data['name']=$userrel['name'];
					$data['profile']=$rel['profile'];
					$forclass=M('class')->where(array('uid'=>$id))->find();
					$class_id=$forclass['class_id'];
					$where['class_id']=$class_id;
					$where['time']=array('GT',time());
					$list=M('liveb')->field('liveb_id,img,name,money')->where($where)->limit(8)->select();
					foreach ($list as $k=>$v){
						$list[$k]['img']=URL.$v['img'];
						$list[$k]['play_id']=$v['liveb_id'];
						$list[$k]['type']='0';
						unset($list[$k]['liveb_id']);
					}
					//交易次数
					$data['deal_num']=M('payrecord')->where(array('uid'=>$id,'type'=>'1'))->count();
					//直播次数
					$livebwhere['class_id']=$class_id;
					$livebwhere['endtime']=array('lt',time());
					$data['liveb_num']=M('liveb')->where($livebwhere)->count();
					//直播列表
					if($list){
						$data['class_details']=$list;
					}else{
						$data['class_details']=array();
					}	
					//直播星级
					$comment_liveb=M('comment_liveb');
					$comment_livebrel=$comment_liveb->where('tid='.$id)->select();
					$comment_liveb_count=$comment_liveb->where('tid='.$id)->count();
					$comment_liveb_means=0;
					if($comment_livebrel){
						foreach($comment_livebrel as $k=>$v){
							$comment_liveb_means+=$v['star'];
	                    	$comment_liveb_mean=round($comment_liveb_means/$comment_liveb_count);
						}
						//平均星级
						$data['comment_mean']=$comment_liveb_mean;
					}else{
						$data['comment_mean']='0';
					}
					
					//视频列表
					$videorel=M('video')->field('video_id,img,name,money,number')->where(array('class_id'=>$class_id))->limit(8)->select();
					foreach($videorel as $k=>$v){
						$videorel[$k]['play_id']=$v['video_id'];
						$videorel[$k]['type']='1';
						$videorel[$k]['buy_num']=$v['number'];
						unset($videorel[$k]['video_id']);

					}
					if($videorel){
						$data['video_list']=$videorel;
					}else{
						$data['video_list']=array();
					}
					echo json_encode($this->apiTemplate($data,'200','ok'));
				}else{
					$data=(object)null;
					echo json_encode($this->apiTemplate($data,'202','不存在该老师'));
				}
				
			}
		}else{
			$data=(object)null;
			echo json_encode($this->apiTemplate($data,'203','请求类型错误'));
		}
	}

// +----------------------------------------------------------------------
// | 功能       | 举报
// +----------------------------------------------------------------------
// | 请求类型   | post
// +----------------------------------------------------------------------
// | 参数       | token,cate,text,img,contact,order_id
// +----------------------------------------------------------------------
	public function report(){
		if(IS_POST){
			$token=I('post.token');
			$cate=I('post.cate');
			$text=I('text');
			$contact=I('contact');
			$img=I('img');
			$order_id=I('post.order_id');
			if($token==''||$cate==''||$text==''||$order_id==''||$img==''){
				$this->templateApi('','204','参数错误');exit;
			}else{
				$user=M('user');
				$userrel=$user->where(array('token'=>$token))->find();
				if($userrel){
					$order=M('order')->where(array('order_id'=>$order_id))->find();
					if($order){
						$pid=$order['pid'];
					}else{
						$this->templateApi('','202','订单不存在');exit;
					}
					$report=M('report');
					$where=array(
						'uid'	=>$userrel['user_id'],
						'cate'	=>$cate,
						'order_id'	=>$pid
					);
					$reportrel=$report->where($where)->find();
					if($reportrel){
						$data=(object)null;
						echo json_encode($this->apiTemplate($data,'202','同一类别禁止多次举报'));
					}else{
						$mvp = array(
							'uid' 		=> $userrel['user_id'],
							'cate'		=> $cate,
							'text'		=> $text,
							'img'		=> $img,
							'contact'	=> $contact,
							'order_id'	=> $pid,
							'time'		=> time(),
						);
						$a=$report->add($mvp);
						$data=(object)null;
						echo json_encode($this->apiTemplate($data,'200','ok'));
					}
				}else{
					$data=(object)null;
					echo json_encode($this->apiTemplate($data,'300','未登录'));
				}
			}
		}else{
			$data=(object)null;
			echo json_encode($this->apiTemplate($data,'203','请求类型错误'));
		}
	}	

}